define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop_money_record/index',
                    multi_url: 'shop_money_record/multi',
                },
                table: 'money_record',
            });
            var table = $("#table");
            table.on('load-success.bs.table', function (e, data) {
                var countData = data.extend.count;
                $('#tj').remove();
                var html = '<div id="tj" class="row" style="padding-top: 10px">';
                $.each(countData,function (index,value) {
                    html = html +  '<div class="table-dashboard-item-container form-group col-xs-12 col-sm-6 col-md-4 col-lg-4"><div class="table-dashboard-item-tip"><span class="table-dashboard-item-dot"></span>'+value.name+':</div>'+value.value+'</div>';
                });
                html = html+'</div>'
                $(".fixed-table-body").append(html);
            });

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                commonSearch: true,
                visible: true,
                showToggle: false,
                showColumns: false,
                search:false,
                searchFormVisible:true,
                showExport: true,
                exportTypes: ['excel'],
                exportOptions: {
                    fileName: '店铺资金明细_'+ Moment().format("YYYY-MM-DD")
                },
                // fixedColumns:true,
                // fixedNumber:3,
                columns: [
                    [
                        {field: 'id', title: 'ID',operate: false},
                        {field: 'shop.shop_name', title: '店铺名称',operate: 'like'},
                        {field: 'agent_name', title: '代理商',operate: false},
                        {field: 'detail_type',title:'账单类型',formatter: Table.api.formatter.normal,searchList: {1: '充值',4:'余额扣除'}},
                        {field: 'orders.order_sn', title: '订单号',operate: 'like'},
                        {field: 'money', title: '变动金额',operate: false,sortable: true,formatter: function (value,row) {
                            if (row.type == 1){
                                return '+'+value;
                            }else {
                                return '-'+value;
                            }
                            }},
                        {field: 'balance',title:'余额',operate: false},
                        {field: 'remark',title:'备注', operate: false},
                        {field: 'createtime', title: '添加时间', formatter: Table.api.formatter.datetime, operate: 'RANGE', addclass: 'datetimerange', sortable: true},
                    ]
                ]
            });

            var options = table.bootstrapTable('getOptions');
            var queryParams = options.queryParams;
            // 绑定TAB事件
            $('.panel-heading a[data-toggle="tab"]').on('shown.bs.tab', function (e) {
                var value = $(this).data("value");
                options = table.bootstrapTable('getOptions');
                options.pageNumber = 1;
                options.queryParams = function (params) {
                    if (value) {
                        params.filter = JSON.stringify(value);
                        let op = {}
                        for (let i in value) {
                            op[i] = '=';
                        }
                        params.op = JSON.stringify(op);
                    }
                    params = queryParams.call(this, params);
                    return params;
                };
                table.bootstrapTable('refresh', {});
                return false;
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        detail: function () {

        },

        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});